package com.translate.web.service.impl;

import com.translate.web.constant.RedisConstant;
import com.translate.web.dao.ServiceIntroductionMapper;
import com.translate.entity.ServiceIntroduction;
import com.translate.vo.contract.IServiceIntroductionVO;
import com.translate.web.service.IServiceIntroductionService;
import com.basic.service.impl.BaseServiceImpl;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import javax.annotation.Resource;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

@Service
public class ServiceIntroductionServiceImpl extends BaseTranslateServiceImpl<ServiceIntroduction>
		implements IServiceIntroductionService {
	
	
	public ServiceIntroductionServiceImpl(ServiceIntroductionMapper mapper) {
		super(mapper);
	}

	protected void insertPropteris(ServiceIntroduction data) {
		long time = System.currentTimeMillis() / 1000;
		data.setCreateTime(time);
		data.setUpdateTime(time);
	}

	protected void updatePropteris(ServiceIntroduction data) {
		data.setUpdateTime(System.currentTimeMillis() / 1000);
	}

	protected Class<ServiceIntroduction> getEntityClass() {
		return ServiceIntroduction.class;
	}


	@Override
	public <VO extends IServiceIntroductionVO> List<VO> getListFromCache(Class<VO> target) {
		return this.entityConvertToVo(this.introductionsToCache(), target);
	}
	
	protected List<ServiceIntroduction> introductionsToCache() {
		
		ValueOperations<String, List<ServiceIntroduction>> opsForValue = (ValueOperations<String, List<ServiceIntroduction>>) redisTemplate.opsForValue();
		
		List<ServiceIntroduction> list = opsForValue.get(RedisConstant.SERVICE_INTRODUCTION);
		
		if (null != list) {
			return list;
		}
		
		list = this.getListByLimit(30);
		
		if (0 == list.size()) {
			opsForValue.set(RedisConstant.SERVICE_INTRODUCTION, list, 3, TimeUnit.SECONDS);
			return list;
		}
		
		opsForValue.set(RedisConstant.SERVICE_INTRODUCTION, list, 6, TimeUnit.SECONDS);

		return list;
	}

	@Resource
	public void setRedisTemplate(RedisTemplate<String, List<ServiceIntroduction>> redisTemplate) {
		this.redisTemplate = redisTemplate;
	}
}